package studio.raptor.ddal.core.log;

import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HostConfiguration;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import studio.raptor.ddal.core.log.common.LogClientConstant;

import java.io.*;

public class LogHttpClient {

	private static Logger logger = LoggerFactory.getLogger(LogHttpClient.class);

    private static MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
	static{
		connectionManager.getParams().setMaxConnectionsPerHost( HostConfiguration.ANY_HOST_CONFIGURATION, LogClientConstant.maxConnection);
		connectionManager.getParams().setMaxTotalConnections(LogClientConstant.maxConnection);
	}

	/**
	 *
	 * @param url
	 * @param timeout
	 * @param inputObj 发送到服务器的对象
	 * @return
	 * @throws IOException
	 */
	public static String sendObj(String url, int timeout, Object inputObj, String appName) throws IOException {
		HttpClient logHttpClient = new HttpClient(connectionManager);
		if(url.indexOf( "?" )<0){
			url=url+"?appName="+appName;
		}else{
			url=url+"&appName="+appName;
		}
		PostMethod post = new PostMethod(url);
		post.setRequestHeader("Content-Type", "application/octet-stream");

		ByteArrayOutputStream bOut = new ByteArrayOutputStream();
		ObjectOutputStream out = null;
		ByteArrayInputStream bInput = null;
		String returnObj = null;
		try {
			logHttpClient.getParams().setConnectionManagerTimeout(timeout );
			logHttpClient.getParams().setSoTimeout(timeout);
			logHttpClient.setConnectionTimeout(timeout);
			logHttpClient.setTimeout(timeout);
			logHttpClient.getParams().setParameter( HttpMethodParams.RETRY_HANDLER,new DefaultHttpMethodRetryHandler(0, false));
			out = new ObjectOutputStream(bOut);
			out.writeObject(inputObj);
			out.flush();
			out.close();
			out = null;
			bInput = new ByteArrayInputStream(bOut.toByteArray());
			RequestEntity re = new InputStreamRequestEntity(bInput);
			post.setRequestEntity(re);

			logger.debug("LogHttpClient.sendObj ......");
			logHttpClient.executeMethod(post);

			InputStream in = post.getResponseBodyAsStream();
			ObjectInputStream oInput = new ObjectInputStream(in);
			returnObj = (String) oInput.readObject();
			oInput.close();
			oInput = null;
		} catch (Exception e) {
			e.printStackTrace();
		}  finally {
			try{
				if (out != null) {
					out.close();
					out = null;
				}
				if (bInput != null) {
					bInput.close();
					bInput = null;
				}
				if(bOut!=null){
					bOut.close();
					bOut = null;
				}
				if(post!=null){
					post.releaseConnection();
				}
			}catch(Exception e){
				e.printStackTrace();
			}
		}
//		logger.debug("LogHttpClient.sendObj return:"+returnObj);
		return returnObj;
	}

}